#include <stdio.h>
#include <stdlib.h>
#include "bilist.h"

//创建头结点
int bilist_create_head(Head_st **ptr)
{
	*ptr = malloc(sizeof(**ptr));
	if(*ptr == NULL)
		return -1;
	(*ptr)->first = NULL;
	(*ptr)->last = NULL;
	(*ptr)->number = 0;
	return 0;
}
//创建链表
int bilist_create_list(Head_st **ptr,enum CUR_MODE way)
{
	if(*ptr == NULL)
		return -1;
	int num;
	while(1)
	{
		scanf("%d",&num);
		if(num == -1)
			break;
		bilist_st *newnode = NULL;
		newnode = malloc(sizeof(*newnode));
		if(newnode == NULL)
			return -1;
		newnode->data = num;
		newnode->prev = NULL;
		newnode->next = NULL;
		switch(way)
		{
			case 1:
				{
					bilist_insert_head(ptr,newnode);	
					break;
				}
			case 2:
				{
					bilist_insert_end(ptr,newnode);	
					break;
				}
			case 3:
				{
					bilist_insert_order(ptr,newnode);	
					break;
				}
		
		}
	}
	return 0;
}	
//三种插入方式
int bilist_insert_head(Head_st **ptr,bilist_st *newnode)
{
	if((*ptr)->first == NULL)
	{
		(*ptr)->first = newnode;
		(*ptr)->last = newnode;
		newnode->prev = newnode->next = NULL;
	}
	else
	{
		newnode->next = (*ptr)->first;
		newnode->next->prev = newnode;
		(*ptr)->first = newnode;
	}
	(*ptr)->number++;
	return 0;	
}

int bilist_insert_end(Head_st **ptr,bilist_st *newnode)
{
	if((*ptr)->first == NULL)
	{
		(*ptr)->first = newnode;
		(*ptr)->last = newnode;
		newnode->prev = newnode->next = NULL;
	}
	else
	{
		(*ptr)->last->next = newnode;
		newnode->prev = (*ptr)->last;
		(*ptr)->last = newnode;
	}
	(*ptr)->number++;	
	return 0;
}

int bilist_insert_order(Head_st **ptr,bilist_st *newnode)
{
	if((*ptr)->first == NULL)
	{
		(*ptr)->first = newnode;
		(*ptr)->last = newnode;
		newnode->prev = newnode->next = NULL;
	}
	else
	{
		bilist_st *cur = (*ptr)->first;
		while(cur)
		{
			if(cur->data > newnode->data)
				break;
			cur = cur->next;
		}
		if(cur == NULL)
		{
			(*ptr)->last->next = newnode;
			newnode->prev = (*ptr)->last;
			(*ptr)->last = newnode;
		}
		else
		{
			if(cur == (*ptr)->first)
			{
				newnode->next = (*ptr)->first;
				newnode->next->prev = newnode;
				(*ptr)->first = newnode;
			}
			else
			{
				newnode->prev = cur->prev;
				newnode->next = cur;
				newnode->prev->next = newnode;
				newnode->next->prev = newnode;
			}
		}
	}
	(*ptr)->number++;
}
//查找函数
bilist_st *bilist_find(Head_st *ptr,const Datatype data)
{
	if(ptr == NULL)
		return NULL;
	bilist_st *p = ptr->first;
	while(p)
	{
		if(p->data == data)
			return p;
		p = p->next;
	}
		return NULL;
}
//删除函数
void bilist_delete(Head_st **ptr,const Datatype data)
{
	bilist_st *x = NULL;
	x = bilist_find(*ptr,data);
	if(x == NULL)
		return ;
	else
	{
		if(x == (*ptr)->first)
		{
			(*ptr)->first = x->next;
			x->next = NULL;
			x->next->prev = NULL;
		}
		else if(x == (*ptr)->last)
		{
			(*ptr)->last = x->prev;
			x->prev->next = NULL;
			x->prev = NULL;
		}
		else
		{
			x->prev->next = x->next;
			x->next->prev = x->prev;
		}
	}
	free(x);
	(*ptr)->number--;
}
//打印函数
void bilist_display(Head_st *ptr)
{
	if(ptr == NULL)
		return ;
	bilist_st *p = NULL;
	for(p = ptr->first;p;p = p->next)
	{
		printf("%d ",p->data);
	}
		printf("\n");
}
//销毁函数
void bilist_distory(Head_st *ptr)
{
	bilist_st *p = NULL;
	bilist_st *next = NULL;
	for(p = ptr->first;p;p = next)
	{
		next = p->next;
		free(p);
	}
	free(ptr);

}
